package com.unitd.frame.comm.cluster;

import org.apache.commons.lang3.RandomStringUtils;

import java.net.InetAddress;
import java.util.UUID;

/**
 * @desc 分布式状态下, 多节点部署的节点名称持有器, 通过他可以获取当前运行环境的节点信息
 * @filename NodeNameHolder.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/4/23
 */
public class NodeNameHolder {

	/** 分布式状态下,多节点部署的节点名称ID */
	private static String nodeId;

	/**
	 * @desc 获取当前服务运行的节点
	 * @return String
	 */
	public static String getNodeId() {
		if (nodeId != null) return nodeId;

		try {
			nodeId = InetAddress.getLocalHost().getHostAddress() + "_" + RandomStringUtils.random(3, true, true).toLowerCase();
		} catch (Exception e) {
			nodeId = UUID.randomUUID().toString();
		}
		return nodeId;
	}
}